from app.window.main_window import Window

from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from app.data import init_database
from app.log import Logger
import logging
import time
import os
from database.db import Database
from qt_material import apply_stylesheet# create the application and the main window



class UiSignal(QObject):
    send_script = Signal(str)

class App():
    def __init__(self):
        super().__init__()

        # init_db()
        init_database()
        self.window = Window()
        self.window.show()
        self.window.init_data()
        
        self.signal = UiSignal()

        self.init_log()
        
        self.init_signal_slot()




        
    def init_work_thread(self):
        pass

  

    # 连接信号槽
    def init_signal_slot(self):
        
        # 设备列表信号 device_widget
        self.window.widget_devices.signal.set_tab.connect(self.window.set_tab)
        self.window.widget_devices.signal.exec_script.connect(self.window.widget_execute.slot_device_excute_script)
        
        # Execute 界面信号
        self.window.widget_execute.signal.send_logs.connect(self.window.dialog_log.append_log)
        


    def init_log(self):
        date = time.localtime()
        date = time.strftime('%Y-%m-%d',date)
        folder = 'Logs'
        if not os.path.exists(folder):
            os.makedirs(folder)

        self.log = Logger()
        self.app_log = logging.getLogger('app_log')
        self.app_log.setLevel(logging.DEBUG)
        log_file = f'{folder}\\{date}.log'
        fh = logging.FileHandler(log_file,mode='a',encoding='utf-8')
        fh.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        self.app_log.addHandler(fh)

        
